package com.example.demo.controller;

import com.example.demo.dto.login.LoginDto;
import com.example.demo.entity.User;
import com.example.demo.service.IUserService;
import com.example.demo.utils.result.Result;
import com.example.demo.utils.result.ResultUtils;
import com.example.demo.vo.login.LoginVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("login")
public class LoginController {

    @Autowired
    IUserService userService;

    @PostMapping("login")
    public Result<LoginVo> login(@RequestBody LoginDto loginDto) {
        LoginVo loginVo = new LoginVo();
        String name = loginDto.getName();
        String password = loginDto.getPassword();
        User user = userService.selectOneByNameAndPassword(name, password).orElse(new User());
        BeanUtils.copyProperties(user, loginVo);
        return ResultUtils.success(loginVo);
    }
}
